
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer



func handler(data: [String:Any]) throws -> RequestHandler {
	return {
		request, response in
		response.setHeader(.contentType, value: "text/html")
		response.appendBody(string: "successsTest")
		response.completed()
	}
}

func changePwd(data: [String:Any]) throws -> RequestHandler {
    return {
        request, response in
        response.setHeader(.contentType, value: "application/json")
        let login = message()
        login.regist(request, response: response)
        
    }
}
func getScore(data: [String:Any]) throws -> RequestHandler {
    return {
        request, response in
        response.setHeader(.contentType, value: "application/json")
        let login = message()
        login.getData(request, response: response)
        
        
    }
}

func getBDJ(data: [String:Any]) throws -> RequestHandler {
    return {
        request, response in
        response.setHeader(.contentType, value: "application/json")
        let login = bdjFile()
        login.getData(request, response: response)
        
        
    }
}

func getAreaID(data: [String:Any]) throws -> RequestHandler {
    return {
        request, response in
        response.setHeader(.contentType, value: "application/json")
        let weather = weatherR()
        weather.getAreaID(request, response: response)
        
        
    }
}


func getCityWeather(data: [String:Any]) throws -> RequestHandler {
    return {
        request, response in
        response.setHeader(.contentType, value: "application/json")
        let weather = weatherR()
        weather.getCityWeather(request, response: response)
        
        
    }
}

let port1 = 8081, port2 = 8181

let confData = [
	"servers": [  
		[
			"name":"localhost",
			"port":port1,
			"routes":[
				["method":"get", "uri":"/", "handler":handler],
				["method":"get", "uri":"/login/changePwd", "handler":changePwd],
				["method":"get", "uri":"/login/getScore", "handler":getScore],
				["method":"get", "uri":"/login/getBDJ", "handler":getBDJ],
				["method":"get", "uri":"/weather/getAreaID", "handler":getAreaID],
				["method":"get", "uri":"/weather/cityWeather", "handler":getCityWeather],
				["method":"get", "uri":"/**", "handler":PerfectHTTPServer.HTTPHandler.staticFiles,
				 "documentRoot":"./webroot",
				 "allowResponseFilters":true],
				
			],
			"filters":[
				[
				"type":"response",
				"priority":"high",
				"name":PerfectHTTPServer.HTTPFilter.contentCompression,
				]
			]
		]

		//[
			//"name":"localhost",
			//"port":port2,
		//	"routes":[
			//	["method":"get", "uri":"/**", "handler":PerfectHTTPServer.HTTPHandler.redirect,
			//	 "base":"http:localhost:\(port1)"]
			//]
		//]

	]
]






do {
	// Launch the servers based on the configuration data.
	try HTTPServer.launch(configurationData: confData)
} catch {
	fatalError("\(error)")
}

